﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWSoft.Framework;
using System.Data;
using System.Xml;
using System.Data.Common;
using MediSIIM.BizComponent.HangZhouSYKT;

namespace HangZhouSYKT
{
    public class EntryPoint : DBVisitor<Entry>
    {
        public DataTable GetTable(string sql, params object[] args)
        {
            return ExecuteTable(string.Format(sql, args));
        }

        public void executesql(string sql)
        {
            DBVisitor.ExecuteNonQuery(sql);
        }

        public const string SQL001 = "SELECT * FROM yb_mzjsxx yz WHERE yz.ylbxid=104 AND NOT EXISTS (SELECT yz.mzjsid FROM yb_mzjsmx yz2 WHERE yz.mzjsid=yz2.mzjsid)";
        public const string SQL002 = "SELECT * FROM yb_zyjsxx yz WHERE yz.ylbxid=104 AND NOT EXISTS (SELECT yz.zyjsid FROM yb_zyjsmx yz2 WHERE yz.zyjsid=yz2.zyjsid)";

        public static List<GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG> GetOut(string outxml, string ybbrxx)
        {
            Dictionary<string, string> dict;
            var olist = GetItems(outxml, out dict);
            var jsjg = new GUAHAOYJS_OUTGUAHAOYJSJIESUANJG();

            jsjg.FEIYONGZE = dict["AKC264"];
            jsjg.ZILIJE = dict["AKC228"];//自理金额
            jsjg.ZIFUJE = (decimal.Parse(dict["AKC740"]) + decimal.Parse(dict["AKC741"]) + decimal.Parse(dict["AKC742"]) + decimal.Parse(dict["AKC743"]) + decimal.Parse(dict["AKC744"]) + decimal.Parse(dict["AKC745"])).ToString();
            jsjg.ZIFEIJE = dict["AKC253"];//自费金额
            jsjg.BAOXIAOJE = (GetDecimal(dict["AKC264"]) - GetDecimal(dict["AKC261"])).ToString();//费用总额-现金支付
            jsjg.XIANJINZF = dict["AKC261"];
            string ybjsxx = string.Empty;
            var list = new List<GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG>();
            for (int i = 0; i < olist.Count; i++)
            {
                var xxjgxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
                var fundname = olist[i];
                if (MappingInfo.FeiyongType.ContainsKey(fundname))
                {
                    xxjgxx.JIEGUOXH = (list.Count + 1).ToString();
                    xxjgxx.JIEGUONR = dict[fundname];
                    xxjgxx.JIEGUOLX = MappingInfo.FeiyongType[fundname][0];
                    xxjgxx.JIEGUODM = MappingInfo.FeiyongType[fundname][1];
                    xxjgxx.JIEGUOMC = MappingInfo.FeiyongType[fundname][2];
                    ybjsxx += xxjgxx.JIEGUONR + "|";
                    list.Add(xxjgxx);
                }
            }

            var strs = ybbrxx.Split('|');
            var newxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
            newxx.JIEGUOXH = (list.Count + 1).ToString();
            newxx.JIEGUOLX = "2";
            newxx.JIEGUODM = "BXZE";
            newxx.JIEGUOMC = "报销总额";
            newxx.JIEGUONR = jsjg.BAOXIAOJE;
            list.Add(newxx);

            newxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
            newxx.JIEGUOXH = (list.Count + 1).ToString();
            newxx.JIEGUOLX = "2";
            newxx.JIEGUODM = "JINE11";
            newxx.JIEGUOMC = "本年账户余额";
            newxx.JIEGUONR = (GetDecimal(ybbrxx.Split('|')[11]) - GetDecimal(dict["AKC255"])).ToString();
            list.Add(newxx);

            newxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
            newxx.JIEGUOXH = (list.Count + 1).ToString();
            newxx.JIEGUOLX = "2";
            newxx.JIEGUODM = "JINE12";
            newxx.JIEGUOMC = "历年账户余额";
            newxx.JIEGUONR = (GetDecimal(strs[12]) - GetDecimal(dict["AKC703"])).ToString();
            list.Add(newxx);

            newxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
            newxx.JIEGUOXH = (list.Count + 1).ToString();
            newxx.JIEGUOLX = "0";
            newxx.JIEGUODM = "GWYBZ";
            newxx.JIEGUOMC = "公务员标志";
            newxx.JIEGUONR = string.IsNullOrEmpty(ybbrxx.Split('|')[15]) ? "0" : ybbrxx.Split('|')[15];
            list.Add(newxx);

            newxx = new GUAHAOYJS_OUTGUAHAOYJSXIANGXIJSJG();
            newxx.JIEGUOXH = (list.Count + 1).ToString();
            newxx.JIEGUOLX = "0";
            newxx.JIEGUODM = "DBBZ";
            newxx.JIEGUOMC = "低保标志";
            newxx.JIEGUONR = string.IsNullOrEmpty(ybbrxx.Split('|')[16]) ? "0" : ybbrxx.Split('|')[16];
            list.Add(newxx);

            return list;
        }

        public static decimal GetDecimal(string value)
        {
            value = value.Trim();
            if (string.IsNullOrEmpty(value))
            {
                return 0M;
            }
            else
            {
                return decimal.Parse(value);
            }
        }

        public static List<string> GetItems(string outxml, out Dictionary<string, string> dict)
        {
            dict = new Dictionary<string, string>();
            var list = new List<string>();
            var xmldom = new XmlDocument();
            xmldom.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + outxml);
            var xmlnode = xmldom.SelectSingleNode("/OUTPUT");
            if (xmlnode != null)
            {
                foreach (XmlNode node in xmlnode.ChildNodes)
                {
                    list.Add(node.Name);
                    dict.Add(node.Name, node.InnerText);
                }
            }
            return list;
        }

        public string GetYb_Sfxmdz(string ylbxid, string ybyydm)
        {
            string sql = string.Format("select SFXMBM from yb_sfxmdz where ylbxid = {0}  and yysfxm ='{1}'", ylbxid, ybyydm);
            DataTable result;
            result = ExecuteTable(sql);
            if (result.Rows.Count > 0)
            {
                return result.Rows[0][0].ToString();
            }
            else
            {
                throw new Exception("SQL执行错误: " + sql);
            }
        }
    }
}
